import { wechatQueryPayResult, localLifePayment } from "./local.js";
import { mapGetters } from "vuex";

export default {
  // computed: mapGetters(['productType']),
  data() {
    return {
      loading: false,
      productType: "",
    };
  },
  methods: {
    /** 显示提示信息 */
    showTips(title, icon = "none", url = "", tab = false) {
      uni.showToast({
        title: title,
        icon: icon,
        duration: 2000,
      });

      if (url) {
        //销毁当前界面
        uni.navigateBack({
          delta: 1,
        });
        //跳转至指定页面
        uni.navigateTo({
          url: url,
        });
      }
    },

    /** 微信或乐刷支付 */
    wechatPay(jsConfig, dataInfo, res) {
      let that = this;
      let leshua = res.data.leshuaPayRequest.leshua_order_id;
      uni.requestPayment({
        timeStamp: jsConfig.timeStamp,
        nonceStr: jsConfig.nonceStr,
        package: jsConfig.packages,
        signType: jsConfig.signType,
        paySign: jsConfig.paySign,
        ticket: that.productType === "normal" ? null : jsConfig.ticket,
        success: function (ress) {
          uni.hideLoading();
          wechatQueryPayResult(dataInfo.orderNo)
            .then((res) => {
              uni.hideLoading();
              uni.request({
                url: "https://www.wantongkonggu.cn/api/frontApph7/order/get/pay/lsReceipt",
                method: "GET",
                data: { leshuaOrderId: leshua },
                success: (res) => {
                  uni.request({
                    url: "https://www.wantongkonggu.cn/api/frontApph7/pay/LsQueryPayResult",
                    method: "GET",
                    data: { orderNo: res.data.data.orderId },
                    success(res) {
                      this.$store.commit("zhifu", 1);
                    },
                  });
                },
                fail: (err) => {
                  that.errdata = JSON.stringify(err);
                },
                complete: (ress) => {},
              });
              return that.showTips("支付成功", "success", dataInfo.goPagesSuccess, dataInfo.tab);
            })
            .catch((err) => {
              uni.hideLoading();
              return that.showTips("支付失败", "success", err);
            });
        },
        fail: function (e) {
          uni.hideLoading();
          if (dataInfo.goPagesFail === "no") {
            return that.showTips("取消支付");
          }
          return that.showTips("取消支付www", "none", dataInfo.goPagesFail, dataInfo.tab);
        },
        complete: function (e) {
          uni.hideLoading();
          //关闭当前页面跳转至订单状态
          if (e.errMsg === "requestPayment:cancel") {
            if (dataInfo.goPagesFail === "no") {
              return that.showTips("取消支付qqqq");
            }
            return that.showTips("取消支付eeee", "none", dataInfo.goPagesFail, dataInfo.tab);
          }
        },
      });

      let data = {
        timestamp: jsConfig.timeStamp,
        nonceStr: jsConfig.nonceStr,
        package: jsConfig.packages,
        signType: jsConfig.signType,
        paySign: jsConfig.paySign,
      };
      that.$wechat
        .pay(data)
        .then((resData) => {
          if (resData.errMsg === "chooseWXPay:cancel") {
            uni.hideLoading();
            if (dataInfo.goPagesFail === "no") {
              return that.showTips("取消支付????");
            }
            return that.showTips("取消支付rrrr", "none", dataInfo.goPagesFail, dataInfo.tab);
          } else {
            if (this.orderType !== "pay_ls") {
              wechatQueryPayResult(dataInfo.orderNo)
                .then((res) => {
                  uni.hideLoading();
                  return that.showTips("支付成功", "success", dataInfo.goPagesSuccess, dataInfo.tab);
                })
                .catch((err) => {
                  uni.hideLoading();
                  return that.showTips("支付失败", "success", err);
                });
            }
          }
        })
        .catch((res) => {
          uni.hideLoading();
          if (dataInfo.goPagesFail === "no") {
            return that.showTips("取消支付2222");
          }
          return that.showTips("取消支付tttt", "none", dataInfo.goPagesFail, dataInfo.tab);
        });
    },
    getOption(dataInfo, res) {
      let that = this;
      let jsConfig = res.data.jsConfig;
      // if (res.data.payType === 'lszf') {
      //   jsConfig = res.data.leshuaPayRequest;
      // }
      this.loading = false;
      switch (res.data.payType) {
        case "weixin":
          this.wechatPay(jsConfig, dataInfo);
          break;
        case "lszfxcx":
          this.wechatPay(jsConfig, dataInfo, res);
          break;
        case "yue":
          uni.hideLoading();
          return that.showTips("支付成功", "success", dataInfo.goPagesSuccess, dataInfo.tab);
          break;
        case "weixinh5":
          uni.hideLoading();
          that.showTips("订单创建成功", "none", dataInfo.goPagesFail, dataInfo.tab);
          setTimeout(() => {
            location.href = jsConfig.mwebUrl + "&redirect_url=" + window.location.protocol + "//" + window.location.host + dataInfo.goPagesFail;
          }, 100);
          break;
      }
    },
    /** 支付接口 */
    getOrderPay(dataInfo) {
      // console.log(dataInfo)
      let that = this;
      // console.log(that.getPayType(that.orderType))
      // return false
      localLifePayment({
        uni: dataInfo.id,
        orderNo: dataInfo.orderNo,
        payChannel: "public",
        payType: "lszfxcx",
        scene: 0, //下单时小程序的场景值
      })
        .then((res) => {
          if (res.code === 200) {
            that.getOption(dataInfo, res);
          } else {
            that.loading = false;
            return that.showTips(res.message);
          }
        })
        .catch((err) => {
          uni.hideLoading();
          this.loading = false;
          return that.showTips(err);
        });
    },
  },
};
